window.onload = function() {
	$("form").submit(function() {
		var data = {};
		$("input[name]").each(function() {
			data[this.name] = $(this).val();
		});

		var msgs = [];
		if (!data.username) {
			msgs.push("Username is required");
		}

		if (!data.password || !data.confirm_password) {
			msgs.push("Password is required");
		}

		if (data.password !== data.confirm_password) {
			msgs.push("Password do not match");
		}

		// send data

		if (data.username) {
			$.ajax({
				url : "user/checkusername",
				type : "POST",
				data : {
					username : data.username
				},
				async : false,
			}).done(function(message) {
				message = JSON.parse(message);
				if (message.success) {

				} else {
					msgs.push("Username exists");

					// var h = $("#error-div").html();
					// $("#error-div").show().html(
					// h + "<br/>" + "username exists");
				}
			});
		}

		if (msgs.length != 0) {
			$("#error-div").show().html(msgs.join("<br/>"));
			return false;
		} else {
			$("#error-div").hide();
			return true;
		}

	});

};